<?php
class ControllerAccountPassword extends Controller {
    
    private $error = array();
    
    public function reset()
    {
        $this->load->model('account/customer');
        
        if (!$this->actionCheck(false,true)) {
            return;
        }
        
        if ($this->validate()) {
            $this->model_account_customer->editPasswordByPhone($this->request->post['phone'], $this->request->post['password']);
            	
            unset($this->session->data['phone_valide_code']);
            
            $this->response->setDataResult(1,"",null);
            return;
        }
        else if (count($this->error)>0) {
            $error_warning = array_shift($this->error);
        } else {
            $error_warning = '';
        }
        $this->response->setDataResult(0,$error_warning,null);
        return;
    }
    
	public function validate() {
	    if((utf8_strlen($this->request->post['code']) < 4) || (utf8_strlen($this->request->post['code']) > 6))
	    {
	        $this->error['code'] = '无效的短信验证码！';
	    }
	    else 
	    {
	        if( !preg_match('/^\d{4}$/i', $this->request->post['code']))
	        {
	            $this->error['code'] = '请输入4位数字短信验证码！';
	        }
	        else if(!isset($this->session->data['phone_valide_code']))
	        {
	            $this->error['code'] = '无效的短信验证码！';
	        }
	        else if(!strpos($this->session->data['phone_valide_code'],$this->request->post['code']))
	        {
	            $this->error['code'] = '短信验证码错误！';
	        }
	    }
	    
	    
	    if (!preg_match('/^\d{11,16}$/i', $this->request->post['phone'])) {
	        $this->error['phone'] = '手机号不合法！';
	    }
		
		if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
		    $this->error['password'] = '请输入4-20位密码！';
		}

		if(!$this->error)
		{
		    if (!$this->model_account_customer->getTotalCustomersByPhone($this->request->post['phone'])) {
		        $this->error['warning'] = '手机号未注册！';
		    }
		}

		return !$this->error;
	}
    
}